home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt030.dms / pt030.adf / Less / Src / os.c < prev    next >
C/C++ Source or Header  |  1987-06-15  |  4KB  |  252 lines

  1. /*
  2.  * Operating system dependent routines.
  3.  *
  4.  * Most of the stuff in here is based on Unix, but an attempt
  5.  * has been made to make things work on other operating systems.
  6.  * This will sometimes result in a loss of functionality, unless
  7.  * someone rewrites code specifically for the new operating system.
  8.  *
  9.  * The makefile provides defines to decide whether various
  10.  * Unix features are present.
  11.  */
  12.  
  13. #include "less.h"
  14. #include <signal.h>
  15.  
  16. char *getenv();
  17.  
  18. /*
  19.  * Pass the specified command to a shell to be executed.
  20.  * Like plain "system()", but handles resetting terminal modes, etc.
  21.  */
  22.     public void
  23. lsystem(cmd)
  24.     char *cmd;
  25. {
  26.     int inp;
  27.     char cmdbuf[256];
  28.     char *shell;
  29.  
  30.     /*
  31.      * Print the command which is to be executed,
  32.      * unless the command starts with a "-".
  33.      */
  34.     if (cmd[0] == '-')
  35.         cmd++;
  36.     else
  37.     {
  38.         lower_left();
  39.         clear_eol();
  40.         putstr("!");
  41.         putstr(cmd);
  42.         putstr("\n");
  43.     }
  44.  
  45.     /*
  46.      * De-initialize the terminal and take out of raw mode.
  47.      */
  48.     deinit();
  49.     flush();
  50.     raw_mode(0);
  51.  
  52. #ifdef AMIGA
  53.     { extern int called_from_WB;
  54.       extern long tty;
  55.  
  56.     if (called_from_WB)
  57.         error("can't execute a CLI command from Workbench, sorry!");
  58.     else
  59.         if (cmd && *cmd)
  60.             /* run a one shot */
  61.             Execute(cmd, 0L, tty);
  62.     }
  63. #else
  64.     /*
  65.      * Restore signals to their defaults.
  66.      */
  67.     SIGNAL(SIGINT, SIG_DFL);
  68. #ifdef SIGTSTP
  69.     SIGNAL(SIGTSTP, SIG_DFL);
  70. #endif
  71.     /*
  72.      * Force standard input to be the terminal, "/dev/tty",
  73.      * even if less's standard input is coming from a pipe.
  74.      */
  75.     inp = dup(0);
  76.     close(0);
  77.     if (open("/dev/tty", 0) < 0)
  78.         dup(inp);
  79.  
  80.     /*
  81.      * Pass the command to the system to be executed.
  82.      */
  83.     if ((shell = getenv("SHELL")) != NULL)
  84.     {
  85.         sprintf(cmdbuf, "%s -c \"%s\"", shell, cmd);
  86.         cmd = cmdbuf;
  87.     }
  88.     system(cmd);
  89.  
  90.     /*
  91.      * Restore standard input, reset signals, raw mode, etc.
  92.      */
  93.     close(0);
  94.     dup(inp);
  95.     close(inp);
  96.  
  97.     init_signals();
  98.     raw_mode(1);
  99.     init();
  100. #endif
  101. }
  102.  
  103. /*
  104.  * Expand a filename, substituting any environment variables, etc.
  105.  * The implementation of this is necessarily very operating system
  106.  * dependent.  This implementation is unabashedly only for Unix systems.
  107.  */
  108. #if GLOB
  109.  
  110. #include <stdio.h>
  111. FILE *popen();
  112.  
  113.     public char *
  114. glob(filename)
  115.     char *filename;
  116. {
  117.     FILE *f;
  118.     char *p;
  119.     int ch;
  120.     static char ECHO[] = "echo ";
  121.     static char filebuf[FILENAME+sizeof(ECHO)+1];
  122.  
  123.     if (filename[0] == '#')
  124.         return (filename);
  125.     strcpy(filebuf, ECHO);
  126.     strtcpy(filebuf+sizeof(ECHO)-1, filename, sizeof(filebuf)-sizeof(ECHO));
  127.     if ((f = popen(filebuf, "r")) == NULL)
  128.         return (filename);
  129.     for (p = filebuf;  p < &filebuf[sizeof(filebuf)-1];  p++)
  130.     {
  131.         if ((ch = getc(f)) == '\n' || ch == EOF)
  132.             break;
  133.         *p = ch;
  134.     }
  135.     *p = '\0';
  136.     pclose(f);
  137.     return (filebuf);
  138. }
  139.  
  140. #else
  141.  
  142.     public char *
  143. glob(filename)
  144.     char *filename;
  145. {
  146.     return (filename);
  147. }
  148.  
  149. #endif
  150.  
  151.  
  152. /*
  153.  * Returns NULL if the file can be opened and
  154.  * is an ordinary file, otherwise an error message
  155.  * (if it cannot be opened or is a directory, etc.)
  156.  */
  157.  
  158. #if STAT
  159.  
  160. #include <sys/types.h>
  161. #include <sys/stat.h>
  162.  
  163.     public char *
  164. bad_file(filename, message, len)
  165.     char *filename;
  166.     char *message;
  167.     int len;
  168. {
  169.     struct stat statbuf;
  170.  
  171.     if (stat(filename, &statbuf) < 0)
  172.         return (errno_message(filename, message, len));
  173.  
  174.     if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
  175.     {
  176.         static char is_dir[] = " is a directory";
  177.         strtcpy(message, filename, len-sizeof(is_dir)-1);
  178.         strcat(message, is_dir);
  179.         return (message);
  180.     }
  181.     if ((statbuf.st_mode & S_IFMT) != S_IFREG)
  182.     {
  183.         static char not_reg[] = " is not a regular file";
  184.         strtcpy(message, filename, len-sizeof(not_reg)-1);
  185.         strcat(message, not_reg);
  186.         return (message);
  187.     }
  188.     return (NULL);
  189. }
  190.  
  191. #else
  192.  
  193.     public char *
  194. bad_file(filename, message, len)
  195.     char *filename;
  196.     char *message;
  197.     int len;
  198. {
  199.     return (NULL);
  200. }
  201.  
  202. #endif
  203.  
  204. /*
  205.  * Return an error message based on the value of "errno".
  206.  */
  207.  
  208. #if PERROR
  209.  
  210.     public char *
  211. errno_message(filename, message, len)
  212.     char *filename;
  213.     char *message;
  214.     int len;
  215. {
  216.     char *p;
  217.     static char msg[16];
  218.  
  219.     extern char *sys_errlist[];
  220.     extern int sys_nerr;
  221.     extern int errno;
  222.  
  223.     if (errno < sys_nerr)
  224.         p = sys_errlist[errno];
  225.     else
  226.     {
  227.         sprintf(msg, "Error %d", errno);
  228.         p = msg;
  229.     }
  230.     strtcpy(message, filename, len-strlen(p)-3);
  231.     strcat(message, ": ");
  232.     strcat(message, p);
  233.     return (message);
  234. }
  235.  
  236. #else
  237.  
  238.     public char *
  239. errno_message(filename, message, len)
  240.     char *filename;
  241.     char *message;
  242.     int len;
  243. {
  244.     static char msg[] = ": cannot open";
  245.  
  246.     strtcpy(message, filename, len-sizeof(msg)-1);
  247.     strcat(message, msg);
  248.     return (message);
  249. }
  250.  
  251. #endif
  252.